﻿//////////////////////////////////////////////////////////////////////////////
//
// Copyright © 1998-2024 Glodon Company Limited.
//
// Licensed under the MIT License
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the “Software”),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
// sell copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////
#include "GbmpSectionViewPositionReporterComponent.h"
#include "IModelView.h"
#include "IGenericElement.h"
#include "GbmpSectionView.h"
#include "IDocument.h"
#include "DbObjectUtils.h"
#include "SectionViewSymbolNew.h"
#include "GbmpModelViewExternalDataId.h"
#include "IExternalDataComponent.h"
#include "EnableCompileWarning_The_LAST_IncludeInCpp.h"


using namespace gcmp;

DBOBJECT_DATA_DEFINE(GbmpSectionViewPositionReporterComponent)
{
    m_pOwnerElement = nullptr;
}

gcmp::GbmpSectionViewPositionReporterComponent::GbmpSectionViewPositionReporterComponent(IElement* pOwnerElement)
    : m_pOwnerElement(pOwnerElement)
{
}

bool gcmp::GbmpSectionViewPositionReporterComponent::GetCoordinate3d(Coordinate3d& coord) const
{
    const SectionViewSymbolNew* pSectionViewSymbol = GetSectionViewSymbol();
    DBG_WARN_AND_RETURN_UNLESS(pSectionViewSymbol, false, L"SectionViewSymbol为空",L"GDMPLab",L"2024-03-30");

    coord = pSectionViewSymbol->GetSectionViewCoord();
    return true;
}

bool gcmp::GbmpSectionViewPositionReporterComponent::GetHeight(double & height) const
{
    const SectionViewSymbolNew* pSectionViewSymbol = GetSectionViewSymbol();
    DBG_WARN_AND_RETURN_UNLESS(pSectionViewSymbol, false, L"SectionViewSymbol为空",L"GDMPLab",L"2024-03-30");

    Coordinate3d coord = pSectionViewSymbol->GetSectionViewCoord();

    height = coord.GetOrigin()*coord.GetZ();
    return true;
}

bool gcmp::GbmpSectionViewPositionReporterComponent::GetReferencePlaneRdId(std::vector<RegenDataId>& dataIds) const
{
    const IModelView* pModelView = quick_cast<IModelView>(m_pOwnerElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");

    const IExternalDataComponent* pExternalDataComponent = pModelView->GetExternalDataComponent();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pExternalDataComponent, L"pExternalDataComponent为空",L"GDMPLab",L"2024-03-30");

    const GbmpSectionView* pSectionView = quick_cast<GbmpSectionView>(pExternalDataComponent->FindExternalData(MODEL_VIEW_EXTERNAL_DATA_ID));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSectionView, L"pSectionView为空",L"GDMPLab",L"2024-03-30");

    dataIds.push_back(pSectionView->GetSectionViewCoordRdId());
    return true;
}

const SectionViewSymbolNew * gcmp::GbmpSectionViewPositionReporterComponent::GetSectionViewSymbol() const
{
    const IModelView* pModelView = quick_cast<IModelView>(m_pOwnerElement);
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pModelView, L"pModelView为空",L"GDMPLab",L"2024-03-30");
    const IDocument* pDoc = pModelView->GetDocument();
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pDoc, L"pDoc为空",L"GDMPLab",L"2024-03-30");

    ElementId symbolId = pModelView->GetViewSymbolId();
    const IGenericElement* pGenericElement =quick_cast<IGenericElement>(pDoc->GetElement(symbolId));
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pGenericElement, L"pGenericElement为空",L"GDMPLab",L"2024-03-30");

    const SectionViewSymbolNew* pSectionViewSymbolNew =quick_cast<SectionViewSymbolNew>(pGenericElement->GetExternalObject());
    DBG_WARN_AND_RETURN_NULLPTR_UNLESS(pSectionViewSymbolNew, L"pSectionViewSymbolNew为空",L"GDMPLab",L"2024-03-30");

    return pSectionViewSymbolNew;
}

